package org.openea.eap.module.obpm.util.form;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;






















public class ArrayUtil
{
  public static Long[] convertArray(String[] aryStr) {
    if (ArrayUtils.isEmpty((Object[])aryStr))
      return ArrayUtils.EMPTY_LONG_OBJECT_ARRAY;
    Long[] aryLong = new Long[aryStr.length];
    for (int i = 0; i < aryStr.length; i++) {
      aryLong[i] = Long.valueOf(Long.parseLong(aryStr[i]));
    }
    return aryLong;
  }








  public static String[] toStringArray(List<String> list) {
    if (CollectionUtils.isEmpty(list)) {
      return null;
    }
    String[] strs = new String[list.size()];
    for (int i = 0; i < list.size(); i++) {
      strs[i] = list.get(i);
    }
    return strs;
  }







  public static String[] convertArray(Long[] aryLong) {
    if (ArrayUtils.isEmpty((Object[])aryLong))
      return ArrayUtils.EMPTY_STRING_ARRAY;
    String[] aryStr = new String[aryLong.length];
    for (int i = 0; i < aryStr.length; i++) {
      aryStr[i] = String.valueOf(aryStr[i]);
    }
    return aryStr;
  }








  public static String contact(String[] aryStr, String separator) {
    if (aryStr.length == 1)
      return aryStr[0];
    String out = "";
    for (int i = 0; i < aryStr.length; i++) {
      if (i == 0) {
        out = out + aryStr[i];
      } else {
        out = out + separator + aryStr[i];
      }
    }
    return out;
  }








  public static String addQuote(String val) {
    String[] aryVal = val.split(",");

    if (aryVal.length == 1) {
      return "'" + val + "'";
    }
    String tmp = "";
    for (int i = 0; i < aryVal.length; i++) {
      if (i == 0) {
        tmp = tmp + "'" + aryVal[i] + "'";
      } else {
        tmp = tmp + ",'" + aryVal[i] + "'";
      }
    }
    return tmp;
  }










  public static List<String> getUniqueList(List<String> list) {
    List<String> tempList = new ArrayList<>();
    for (String i : list) {
      if (!tempList.contains(i)) {
        tempList.add(i);
      }
    }
    return tempList;
  }












  public static boolean contains(int[] arr, int targetValue) { return Arrays.asList(new int[][] { arr }).contains(Integer.valueOf(targetValue)); }


  public static String join(Object[] o, String flag) {
    StringBuffer str_buff = new StringBuffer();

    for (int i = 0, len = o.length; i < len; i++) {
      str_buff.append(String.valueOf(o[i]));
      if (i < len - 1) {
        str_buff.append(flag);
      }
    }
    String str = str_buff.toString();
    if (flag.startsWith("'")) {
      str = "'" + str + "'";
    }
    if (flag.startsWith("\"")) {
      str = "\\\"" + str + "\\\"";
    }

    return str;
  }

  public static String join(List<String> o, String flag) {
    StringBuffer str_buff = new StringBuffer();

    for (int i = 0, len = o.size(); i < len; i++) {
      str_buff.append(String.valueOf(o.get(i)));
      if (i < len - 1) {
        str_buff.append(flag);
      }
    }
    return str_buff.toString();
  }










  public static boolean contains(String[] arrays, String cntype) {
    if (arrays == null) {
      return false;
    }
    for (String string : arrays) {
      if (string.equals(cntype)) {
        return true;
      }
    }
    return false;
  }
}
